package com.yc.book.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.yc.book.biz.BizException;
import com.yc.book.biz.ReaderBiz;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class LoginWin {

	protected Shell shell;
	private Text text;
	private Text text_1;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginWin window = new LoginWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(623, 470);
		shell.setText("登录窗口");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(30, 109, 90, 24);
		lblNewLabel.setText("用户名:");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(149, 106, 383, 30);
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setText("密码:");
		lblNewLabel_1.setBounds(40, 149, 90, 24);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(149, 142, 383, 30);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		
		btnNewButton.setBounds(255, 235, 114, 34);
		btnNewButton.setText("登录");
		
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				ReaderBiz biz = new ReaderBiz();
				String name = text.getText();
				String pwd = text_1.getText();
				try {
					biz.login(name, pwd);
					//关闭窗口
					LoginWin.this.shell.close();
					//登录成功
					new MainWin().open();
				} catch (BizException e1) {
					e1.printStackTrace();
					MessageBox mb = new MessageBox(shell);
					mb.setText("系统提示");
					mb.setMessage(e1.getMessage());
					mb.open();
				}
			}
		});

	}
}
